home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 October
/
CHIP Turkiye Ekim 2000.iso
/
prog
/
naps
/
04
/
setup.exe
/
Gnucleus
/
Packet.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-06-30
|
9KB
|
375 lines
/********************************************************************************
Gnucleus - A node application for the Gnutella network
Copyright (C) 2000 John Marshall
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For support, questions, comments, etc...
E-Mail:
swabby@c0re.net
Address:
21 Cadogan Way
Nashua, NH, USA 03062
********************************************************************************/
#include "stdafx.h"
// Get rid of ugly warnings
#pragma warning (disable : 4786)
#include <map>
// include version functions
#pragma comment(lib, "Version.lib")
#include "Gnucleus.h"
#include "Packet.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
int QueryItem::SortBy = 0;
int QueryItem::Reverse = 0;
XWORD flipX(XWORD in)
{
XWORD out = {in.a, in.b, in.c ,in.d};
return out;
}
WORD flipW(WORD in)
{
byte *out = (byte *) ∈
byte temp = out[0];
out[0] = out[1];
out[1] = temp;
return (WORD) *out;
}
XWORD makeX(DWORD conv)
{
XWORD *in = (XWORD *) &conv;
XWORD out = {in->a, in->b, in->c ,in->d};
return out;
}
DWORD makeD(XWORD conv)
{
DWORD *in = (DWORD *) &conv;
DWORD out = *in;
return out;
}
CString IPtoStr(IP in)
{
// AP: Profiling showed this taking alot of CPU time because it's called alot. Much faster this way.
// char buffer[20];
// ::sprintf (buffer, "%d.%d.%d.%d", in.a, in.b, in.c, in.d);
char * buffer;
buffer = inet_ntoa(*(in_addr *)&in);
return buffer;
}
IP StrtoIP(CString in)
{
/*
int a = 0, b = 0, c = 0, d = 0;
::sscanf (in, "%d.%d.%d.%d", &a, &b, &c, &d);
*/
IP out;
/*
out.a = a;
out.b = b;
out.c = c;
out.d = d;
*/
out.S_addr = inet_addr(in); // standard WinSock2 function
return out;
}
CString WrdtoStr(WORD in)
{
char buff[8];
::sprintf (buff, "%u", in);
return buff;
}
CString DWrdtoStr(DWORD in)
{
char buff[16];
::sprintf (buff, "%u", in);
return buff;
}
CString CommaIze(CString in)
{
if (in.GetLength() > 3)
return CommaIze(in.Left(in.GetLength() - 3)) + "," + in.Right(3);
else
return in;
}
HICON GetIconFromName(CString File)
{
DWORD dwBufLen = 255;
HKEY hKey = NULL;
HKEY pKey = NULL;
HICON FileIcon = NULL;
// Open icon key
if(RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT( File.Mid( File.ReverseFind('.'))), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
TCHAR szFileKey[255];
RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)szFileKey, &dwBufLen);
CString FileKey(szFileKey);
FileKey += "\\DefaultIcon";
// Use the default key to open the icon name dir in the registry
if(RegOpenKeyEx(HKEY_CLASSES_ROOT, FileKey, 0, KEY_QUERY_VALUE, &pKey) == ERROR_SUCCESS)
{
TCHAR szPathKey[255];
dwBufLen = 255;
RegQueryValueEx(pKey, NULL, NULL, NULL, (LPBYTE)szPathKey, &dwBufLen);
CString PathKey(szPathKey);
int comma = PathKey.Find(',');
// Extract the icon
if(ExtractIconEx(PathKey.Mid(0, comma), atoi(PathKey.Mid(comma + 1)), NULL, &FileIcon, 1) == 0)
FileIcon = NULL;
RegCloseKey(pKey);
}
RegCloseKey(hKey);
}
// If we didn't find the icon display a default windows one
if(FileIcon == NULL)
{
TCHAR szSysDir[MAX_PATH];
GetSystemDirectory(szSysDir, sizeof (szSysDir));
CString SysDir(szSysDir);
// Check for .exe
CString testExe = File.Mid( File.ReverseFind('.'));
testExe.MakeLower();
if(testExe == ".exe")
ExtractIconEx(SysDir + "\\shell32.dll", 2, NULL, &FileIcon, 1);
else
ExtractIconEx(SysDir + "\\shell32.dll", 0, NULL, &FileIcon, 1);
}
return FileIcon;
}
CString GetIconDesc(CString File)
// Needs to be merged with the above function
{
DWORD dwBufLen = 255;
HKEY hKey = NULL;
HKEY pKey = NULL;
CString IconDesc;
// Open icon key
if(RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT( File.Mid( File.ReverseFind('.'))), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
TCHAR szFileKey[255];
RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)szFileKey, &dwBufLen);
CString FileKey(szFileKey);
// Use the default key to open the icon name dir in the registry
if(RegOpenKeyEx(HKEY_CLASSES_ROOT, FileKey, 0, KEY_QUERY_VALUE, &pKey) == ERROR_SUCCESS)
{
TCHAR szPathKey[255];
dwBufLen = 255;
RegQueryValueEx(pKey, NULL, NULL, NULL, (LPBYTE)szPathKey, &dwBufLen);
IconDesc = szPathKey;
RegCloseKey(pKey);
}
RegCloseKey(hKey);
}
// If we didn't find the icon display a default windows one
if(IconDesc == "")
{
IconDesc = File.Mid( File.ReverseFind('.') + 1 );
IconDesc.MakeUpper();
IconDesc += " File";
}
return IconDesc;
}
CImageList g_imageList;
std::map<CString,int> g_extensionIndexMap;
int GetIconIndexFromName (const CString& file)
{
CString extension = file.Mid( file.ReverseFind('.'));
extension.MakeLower ();
std::map<CString,int>::iterator it;
it = g_extensionIndexMap.find (extension);
if (it != g_extensionIndexMap.end ())
{
return (*it).second;
}
HICON icon = GetIconFromName (file);
// ASSERT (icon); // We'd better have something!
int index = -1;
if (icon)
{
index = g_imageList.Add (icon);
}
g_extensionIndexMap[extension] = index;
return index;
}
CImageList* GetSharedImageList ()
{
static bool sInited = false;
if (!sInited)
{
g_imageList.Create (16, 16, ILC_COLOR8 | TRUE, 0, 10);
sInited = true;
}
return &g_imageList;
}
bool SearchMatch(const char* _psz_search_for, const char* _psz_string_to_search)
{
for (;;)
{
switch (*_psz_search_for)
{
default:
if (*_psz_search_for != *_psz_string_to_search) // if different chars
return false;
if (!*_psz_search_for) // if equal and both null else fall through
return true;
case '?':
if (!*_psz_string_to_search) // if at end
return false;
_psz_search_for++;
_psz_string_to_search++;
break; // ok, both advanced one
case '*':
_psz_search_for++;
for (;;)
if ( SearchMatch(_psz_search_for, _psz_string_to_search) ) // if rest matches
return true;
else if (*_psz_search_for == '.' && *(_psz_search_for + 1) == '*') // if ".*" or "*.*"
return true;
else if (!*_psz_string_to_search) // if nothing left
return false;
else
++_psz_string_to_search; // try again one char further
}
}
}
//////////////////////////////////////////////////////////////
// Auther: Nathan Brown
//
// Returns the file version number for the passed file name
DWORD64 util_GetVersionNumber(LPCTSTR lpstrFilename)
{
DWORD dwHandle, dwVersionLen;
char strSubBlock[] = "\\"; // the root block, VS_FIXEDFILEINFO
char strShortFilename[129];
WORD * lpData;
WORD * lplpBuffer;
UINT puLen;
DWORD64 dw64FileVersion;
// shorten the path (short filenames)
if(strlen(lpstrFilename) > 127)
{
DWORD retStringLen = GetShortPathName(lpstrFilename, strShortFilename, 128);
}
else
{
strcpy(strShortFilename, lpstrFilename);
}
dwVersionLen = GetFileVersionInfoSize(strShortFilename, &dwHandle);
lpData = new WORD[dwVersionLen];
if(!GetFileVersionInfo(strShortFilename, dwHandle, dwVersionLen, lpData))
{
return false; // if not succede
}
if(!VerQueryValue(lpData, strSubBlock, (VOID**)&lplpBuffer, &puLen))
{
return false;
}
DWORD64 dwFileVersionLS = (((VS_FIXEDFILEINFO*)(lplpBuffer))->dwFileVersionLS);
DWORD64 dwFileVersionMS = (((VS_FIXEDFILEINFO*)(lplpBuffer))->dwFileVersionMS);
dw64FileVersion = (dwFileVersionLS) + (dwFileVersionMS << 32);
delete [] lpData;
return dw64FileVersion;
}